//
//  FMDatePickerView.m
//  FMFormSubmitKit_Example
//
//  Created by 郑桂华 on 2020/6/30.
//  Copyright © 2020 zhoufaming251@163.com. All rights reserved.
//

#import "FMDatePickerView.h"
#import "FMPickerConfigure.h"

@implementation FMDatePickerView

+ (instancetype)showTitle:(id)title dateMode:(UIDatePickerMode)mode date:(NSDate *)date maxDate:(NSDate *)maxDate minDate:(NSDate *)minDate complete:(FMDatePickerSureBlock)sureBlock{
    FMDatePickerView *show = [self show];
    [show showTitle:title dateMode:mode date:date maxDate:maxDate minDate:minDate complete:sureBlock];
    return show;
}

- (void)showTitle:(id)title dateMode:(UIDatePickerMode)mode date:(NSDate  * _Nullable )date maxDate:(NSDate * _Nullable)maxDate minDate:(NSDate * _Nullable)minDate complete:(FMDatePickerSureBlock)sureBlock{
    [self updateTitle:title];
    self.datePicker.datePickerMode = mode;
    self.datePicker.maximumDate = maxDate;
    self.datePicker.minimumDate = minDate;
    self.datePicker.date = date;
    self.sureBlock = sureBlock;
}

- (void)initUI{
    [super initUI];
    UIDatePicker *picker = [[UIDatePicker alloc] init];
    picker.datePickerMode = UIDatePickerModeDate;
    if (@available(iOS 13.4, *)) {
        picker.preferredDatePickerStyle = self.configure.datePickerStyle;
    }
    if (self.configure.magnifierLineColor) {
        [picker setValue:self.configure.magnifierLineColor forKey:@"magnifierLineColor"];
    }
    if (self.configure.textColor) {
        [picker setValue:self.configure.textColor forKey:@"textColor"];
    }
    if (self.configure.highlightColor) {
        [picker setValue:self.configure.highlightColor forKey:@"highlightColor"];
    }
    SEL selector = NSSelectorFromString( @"setHighlightsToday:" );
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature :
                               [UIDatePicker
                                instanceMethodSignatureForSelector:selector]];
    BOOL no = NO;
    [invocation setSelector:selector];
    [invocation setArgument:&no atIndex:2];
    [invocation invokeWithTarget:picker];
    [self.pickerContentView addSubview:picker];
    self.datePicker = picker;
}

- (void)updateWithConfigure:(FMPickerConfigure *)configure{
    if (configure.magnifierLineColor) {
        [self.datePicker setValue:configure.magnifierLineColor forKey:@"magnifierLineColor"];
    }
    if (configure.textColor) {
        [self.datePicker setValue:configure.textColor forKey:@"textColor"];
    }
    if (configure.highlightColor) {
        [self.datePicker setValue:configure.highlightColor forKey:@"highlightColor"];
    }
    [super updateWithConfigure:configure];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.datePicker.frame = self.pickerContentView.bounds;
//    [self updateItemLabelsColor];
}

- (void)sureBtnClick:(UIButton *)sender{
    if (self.sureBlock) {
        self.sureBlock(self.datePicker.date);
    }
    [super sureBtnClick:sender];
}

//- (void)updateItemLabelsColor{
//    UIView *firstView = self.subviews.firstObject;
//    for (UIView *columnView in firstView.subviews) {
//        if ([columnView isKindOfClass:NSClassFromString(@"UIPickerColumnView")]) {
//            for (UIView *view in columnView.subviews.firstObject.subviews) {
//                if ([view.subviews.firstObject isKindOfClass:NSClassFromString(@"UIPickerTableView")]) {
//                    UIView *pickerTableView = view.subviews.firstObject;
//                    for (UIView *view in pickerTableView.subviews) {
//                        if ([view isKindOfClass:NSClassFromString(@"UIPickerTableViewWrapperCell")]) {
//                            UILabel *label = view.subviews.firstObject.subviews.firstObject.subviews.firstObject;
//                            if ([label isKindOfClass:[UILabel class]]) {
//                                label.textColor = self.configure.textColor;
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//}

@end
